//
//  LoginViewController.m
//  HYSmileiOS
//
//  Created by alekye on 16/4/23.
//  Copyright © 2016年 heyou. All rights reserved.
//

#import "LoginViewController.h"
#import "heyou.h"
#import "LoginLogoCell.h"
#import "LoginTextCell.h"
#import "UserManager.h"
#import "HYNotifications.h"

#define K_LOGIN_WEIXIN      @"微信登录"
#define K_LOGIN_WEIBO       @"微博登录"
#define K_LOGIN_QQ          @"QQ登录"

@interface LoginViewController ()
- (IBAction)onCancelButtonClick:(id)sender;
@property (strong, nonatomic) NSArray<NSString*> *mLoginList;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setupNotification];
    [self prepareData];
    [self setupTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- Notification
- (void)setupNotification {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(onLoginSucess:) name:USER_LOGIN_SUCCESS object:nil];
    [defaultCenter addObserver:self selector:@selector(onLoginFailure:) name:USER_LOGIN_FAILURE object:nil];
    [defaultCenter addObserver:self selector:@selector(onLogoutDone:) name:USER_LOGOUT_DONE object:nil];
}

- (void)onLoginSucess:(NSNotification*)notify {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onLoginFailure:(NSNotification*)notify {
}

- (void)onLogoutDone:(NSNotification*)notify {
}

- (void)dealloc {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter removeObserver:self];
}

#pragma mark -- prepare data
- (void)prepareData {
    NSArray<NSString*> *list = @[K_LOGIN_WEIXIN, K_LOGIN_WEIBO, K_LOGIN_QQ];
    self.mLoginList = list;
}

- (void)setupTableView {
    // 注册XIB
    [XibUtils registXibCell:[LoginLogoCell class] toTable:self.tableView];
    [XibUtils registXibCell:[LoginTextCell class] toTable:self.tableView];
    self.tableView.tableFooterView = [UIView new];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger num = self.mLoginList.count;
    
    num += 1;
    
    return num;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 0.0f;
    
    if (indexPath.row == 0) {
        height = 100.0f;
    } else {
        height = 50.0f;
    }
    
    height = MAX(height, 45.0f);
    return height;
}

#pragma mark -- config tableview
- (void)configCell:(UITableViewCell*)cell forIndex:(NSIndexPath*)indexPath {
    if (indexPath.row == 0) {
        
    } else {
        NSInteger index = indexPath.row - 1;
        NSString *title = [self.mLoginList objectAtIndex:index];
        LoginTextCell *tCell = (LoginTextCell*)cell;
        tCell.labelTitle.text = title;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    
    if (indexPath.row == 0) {
        NSString *identifier = NSStringFromClass([LoginLogoCell class]);
        LoginLogoCell *tCell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        
        cell = tCell;
    } else {
        NSString *identifier = NSStringFromClass([LoginTextCell class]);
        LoginTextCell *tCell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        
        cell = tCell;
    }
    
    // Configure the cell...
    [self configCell:cell forIndex:indexPath];
    
    return cell;
}

#pragma mark -- select cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0) {
        
    } else {
        NSInteger index = indexPath.row - 1;
        NSString *title = [self.mLoginList objectAtIndex:index];
        NSLog(@"login === %@", title);
        if ([title isEqualToString:K_LOGIN_WEIXIN]) {
            
        } else if ([title isEqualToString:K_LOGIN_WEIBO]) {
//            [[UserManager sharedInstance] userLogin:LoginType_Weibo];
            [[UserManager sharedInstance] login:SSDKPlatformTypeSinaWeibo];
        } else if ([title isEqualToString:K_LOGIN_QQ]) {
            
        }
    }
}

/**
 *  关闭Login模块
 *
 *  @param sender 关闭按钮
 */
- (IBAction)onCancelButtonClick:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
@end
